home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / TextureByReference / Tetrahedron.java.z / Tetrahedron.java
Encoding:
Java Source  |  2003-08-08  |  7.1 KB  |  217 lines

  1. /*
  2.  *    @(#)Tetrahedron.java 1.4 02/04/01 15:04:08
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import javax.media.j3d.*;
  41. import javax.vecmath.*;
  42.  
  43.  
  44. public class Tetrahedron extends Shape3D {
  45.  
  46.   private static final float sqrt3 = (float) Math.sqrt(3.0);
  47.   private static final float sqrt3_3 = sqrt3 / 3.0f;
  48.   private static final float sqrt24_3 = (float) Math.sqrt(24.0) / 3.0f;
  49.   
  50.   private static final float ycenter = 0.5f * sqrt24_3;
  51.   private static final float zcenter = -sqrt3_3;
  52.   
  53.   private static final Point3f p1 = new Point3f(-1.0f, -ycenter, -zcenter);
  54.   private static final Point3f p2 = new Point3f(1.0f, -ycenter, -zcenter);
  55.   private static final Point3f p3 = new Point3f(0.0f, -ycenter, -sqrt3 - zcenter);
  56.   private static final Point3f p4 = new Point3f(0.0f, sqrt24_3 - ycenter, 0.0f);
  57.   
  58.   private static final Point3f[] verts = {
  59.     p1, p2, p4,    // front face
  60.     p1, p4, p3,    // left, back face
  61.     p2, p3, p4,    // right, back face
  62.     p1, p3, p2,    // bottom face
  63.   };
  64.  
  65.   private Point2f texCoord[] = {
  66.     new Point2f(-0.25f, 0.0f),
  67.     new Point2f(1.25f, 0.0f),
  68.     new Point2f(0.5f, 2.0f),
  69.   };
  70.  
  71.   private TriangleArray geometryByRef;
  72.   private TriangleArray geometryByCopy;
  73.  
  74.   // for geometry by reference
  75.   private Point3f[] verticesArray = new Point3f[12];
  76.   private TexCoord2f[] textureCoordsArray = new TexCoord2f[12];
  77.   private Vector3f[] normalsArray = new Vector3f[12];
  78.  
  79.   // default to geometry by copy
  80.   public Tetrahedron() {
  81.     this(false);
  82.   }
  83.  
  84.   // creates a tetrahedron with geometry by reference or by copy depending on
  85.   // the byRef parameter
  86.   public Tetrahedron(boolean byRef) {
  87.     if (byRef) {
  88.       createGeometryByRef();
  89.       this.setGeometry(geometryByRef);
  90.     }
  91.     else {
  92.       createGeometryByCopy();
  93.       this.setGeometry(geometryByCopy);
  94.     }
  95.     this.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
  96.     this.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
  97.     setAppearance(new Appearance());
  98.   }
  99.  
  100.   // create the geometry by reference and 
  101.   // store it in the geometryByRef variable
  102.   public void createGeometryByRef() {
  103. //     System.out.println("createGeometryByRef");
  104.     geometryByRef = new TriangleArray(12, TriangleArray.COORDINATES |
  105.                       TriangleArray.NORMALS |
  106.                       TriangleArray.TEXTURE_COORDINATE_2 |
  107.                       TriangleArray.BY_REFERENCE);
  108.  
  109.     int i;
  110.  
  111.     // the coordinates
  112.     for (i = 0; i < 12; i++) {
  113.       verticesArray[i] = new Point3f(verts[i]);
  114.     }
  115.     geometryByRef.setCoordRef3f(verticesArray);
  116. //     System.out.println("coordinates set");
  117. //     Point3f[] temp1 = geometryByRef.getCoordRef3f();
  118. //     for (i = 0; i < 12; i++) {
  119. //        System.out.println(temp1[i]);
  120. //     }
  121.  
  122.     // the texture coordinates
  123.     for (i = 0; i < 12; i++) {
  124.       textureCoordsArray[i] = new TexCoord2f(texCoord[i%3]);
  125.     }
  126.     geometryByRef.setTexCoordRef2f(0, textureCoordsArray);
  127. //     System.out.println("texture coords set");
  128. //     TexCoord2f[] temp2 = geometryByRef.getTexCoordRef2f(0);
  129. //     for (i = 0; i < 12; i++) {
  130. //       System.out.println(temp2[i]);
  131. //     }
  132.  
  133.     // the normals
  134.     Vector3f normal = new Vector3f();
  135.     Vector3f v1 = new Vector3f();
  136.     Vector3f v2 = new Vector3f();
  137.     Point3f[] pts = new Point3f[3];
  138.     for (int face = 0; face < 4; face++) {
  139.       pts[0] = new Point3f(verts[face*3]);
  140.       pts[1] = new Point3f(verts[face*3+1]);
  141.       pts[2] = new Point3f(verts[face*3+2]);
  142.       v1.sub(pts[1], pts[0]);
  143.       v2.sub(pts[2], pts[0]);
  144.       normal.cross(v1, v2);
  145.       normal.normalize();
  146.       for (i = 0; i < 3; i++) {
  147.     normalsArray[face*3+i] = new Vector3f(normal);
  148.       }
  149.     }
  150.     geometryByRef.setNormalRef3f(normalsArray);
  151. //     System.out.println("normals set");
  152. //     Vector3f[] temp3 = geometryByRef.getNormalRef3f();
  153. //     for (i = 0; i < 12; i++) {
  154. //       System.out.println(temp3[i]);
  155. //     }
  156.   }
  157.  
  158.   // create the geometry by copy and store it in the geometryByCopy variable
  159.   public void createGeometryByCopy() {
  160.      int i;
  161.      geometryByCopy = new TriangleArray(12, TriangleArray.COORDINATES |
  162.                         TriangleArray.NORMALS | 
  163.                         TriangleArray.TEXTURE_COORDINATE_2);
  164.  
  165.      geometryByCopy.setCoordinates(0, verts);
  166.  
  167.      for (i = 0; i < 12; i++) {
  168.        geometryByCopy.setTextureCoordinate(0, i, 
  169.                        new TexCoord2f(texCoord[i%3]));
  170.      }
  171.      
  172.      int face;
  173.      Vector3f normal = new Vector3f();
  174.      Vector3f v1 = new Vector3f();
  175.      Vector3f v2 = new Vector3f();
  176.      Point3f [] pts = new Point3f[3];
  177.      for (i = 0; i < 3; i++) pts[i] = new Point3f();
  178.     
  179.      for (face = 0; face < 4; face++) {
  180.        geometryByCopy.getCoordinates(face*3, pts);
  181.        v1.sub(pts[1], pts[0]);
  182.        v2.sub(pts[2], pts[0]);
  183.        normal.cross(v1, v2);
  184.        normal.normalize();
  185.        for (i = 0; i < 3; i++) {
  186.      geometryByCopy.setNormal((face * 3 + i), normal);
  187.        }
  188.      }
  189.    }
  190.  
  191.   // set the geometry to geometryByRef or geometryByCopy depending on the
  192.   // parameter.  Create geometryByRef or geometryByCopy if necessary  
  193.   public void setByReference(boolean b) {
  194. //     System.out.println("Tetrahedron.setByReference " + b);
  195.     // by reference is true
  196.     if (b) {
  197.       // if there is no geometryByRef, create it
  198.       if (geometryByRef == null) {
  199.     createGeometryByRef();
  200.       }
  201.       // set the geometry
  202.       this.setGeometry(geometryByRef);
  203.     }
  204.     // by reference is false 
  205.     else {
  206.       // if there is no geometryByCopy, create it
  207.       if (geometryByCopy == null) {
  208.     createGeometryByCopy();
  209.       }
  210.       // set the geometry
  211.       this.setGeometry(geometryByCopy);
  212.     }      
  213.   }
  214. }
  215.     
  216.     
  217.